package com.xyebank.ledger.service.xyd.adaper;

import java.util.Date;

import com.xyebank.ledger.util.DateConverter;
import org.modelmapper.AbstractConverter;
import org.modelmapper.Converter;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.stereotype.Component;

import com.xyebank.ledger.db.entitys.LPaymentInf;
import com.xyebank.ledger.service.common.conveter.PayBeanConverter;
import com.xyebank.ledger.service.constant.CommonConstant;
import com.xyebank.ledger.service.constant.ProductConstant;
import com.xyebank.ledger.util.DateUtil;
import com.xyebank.ledger.util.IDUtil;
import com.xyebank.ledger.vo.deal.XYDCashCreditVo;

@Component(value = "xydCreditBeanConverter")
public class XYDCreditBeanConverter implements PayBeanConverter<XYDCashCreditVo> {

	ModelMapper modelMapper = new ModelMapper();

	// 不能采取注入方式
	private DateConverter dateConverter = new DateConverter();

	class NXApplyPropertyMap extends PropertyMap<XYDCashCreditVo, LPaymentInf> {
		// 注意 不要有重复的映射，不然会报错
		protected void configure() {
			// 享宇订单号
			map(source.getOutorderNumber(), destination.getXyOrderNo());
			// 申请人电话号码
			map(source.getPrimaryPhone(), destination.getMblNo());
			// 提现审批状态
			// map(source.getStatus(), destination.getApprovalStatus());
			// 放款状态
			map(source.getStatus(), destination.getLoanStatus());
			// 放款金额
			map(source.getAmount(), destination.getLoanAmount());
			// 创建时间
			map(source.getApplyterm(), destination.getLoanPeriod());

		}
	}

	public XYDCreditBeanConverter() {
		modelMapper.addMappings(new NXApplyPropertyMap());
	}

	@Override
	public LPaymentInf covert(XYDCashCreditVo x) {
		LPaymentInf entity = modelMapper.map(x, LPaymentInf.class);
		entity.setDataSource(x.getOutorderNumber().length() == 16 ? CommonConstant.JRY_STR : CommonConstant.SJD_STR);
		entity.setOrgNo(ProductConstant.XYD_LOAN_ORG);
		entity.setProductNo( ProductConstant.XYD_LOAN_PROD_ID);
		entity.setId(IDUtil.generateId(x.getId().toString(), "xyd_creat"));
		String status = x.getStatus();
		if ("success".equals(status) || "AUDIT_SUCCESS".equals(status)) {
			entity.setApprovalStatus(CommonConstant.APPLY_SUCCESS);
		} else {
			entity.setApprovalStatus(CommonConstant.APPLY_FAIL);
		}
		return entity;
	}
}
